package com.minivision.sdktestdemo.ui.view;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.widget.PopupWindow;

import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.bean.KeyBean;
import com.minivision.sdktestdemo.bean.MusicBean;
import com.minivision.sdktestdemo.event.PlayMusicEvent;
import com.minivision.sdktestdemo.theme.utils.ThemeUtils;

import java.util.List;

/**
 * Created by devtang on 2016/5/13.
 */
public class PopupView {

    private PopupWindow mPopupWindow;
    private PopContentView mContentView;
    protected Activity mContext;

    public PopupView(Activity context, PopContentView v) {
        mContext = context;
        mContentView = v;
        initContentListener();
        initPopupWindow();
    }

    private final View.OnTouchListener LISTENER = new View.OnTouchListener() {
        private float y;
        private float by;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    int offY = (int) (event.getRawY() - y);
                    if (event.getRawY() <= y) {
                        //不能超过popupwind的最大高度
                        y = event.getRawY();
                    } else {
                        v.setTranslationY(by + offY);
                    }
                    break;
                case MotionEvent.ACTION_DOWN:
                    by = v.getTranslationY();
                    y = event.getRawY();
                    break;
                case MotionEvent.ACTION_UP:
                    int pos = (int) v.getTranslationY();
                    if (pos * 2 > v.getHeight()) {
                        mPopupWindow.dismiss();
//                        v.setTranslationY(by);
                    } else {

//                        v.setTranslationY(by);
                    }
                    v.animate().translationY(by).setDuration(300).start();
                    break;
            }
            return true;
        }
    };

    private View.OnTouchListener mListener;

    void setTouchListener(View.OnTouchListener listener) {
        mListener = listener;
        mContentView.getContentView().setOnTouchListener(mListener);
    }

    private void initContentListener() {
        View v = mContentView.getContentView();
        v.setOnTouchListener(LISTENER);
    }

    private void initPopupWindow() {
        mPopupWindow = new PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, mContentView.getMaxPopWindowHeight());
        mPopupWindow.setContentView(mContentView.getContentView());
        //设置为点击其他区域则让popupwindow消失
        mPopupWindow.setFocusable(true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());

        mPopupWindow.getContentView().setFocusableInTouchMode(true);
        mPopupWindow.getContentView().setFocusable(true);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                startUpdateBackAlpha(false);
            }
        });
        //给popupwindow设置动画
        mPopupWindow.setAnimationStyle(R.style.popmenuStyle);
        //
        mContentView.setPopupWindow(mPopupWindow);
    }

    void show(final int id) {
        mContext.findViewById(id).post(new Runnable() {
            @Override
            public void run() {
                ThemeUtils.initTheme(mPopupWindow.getContentView());
                mPopupWindow.showAtLocation(mContext.findViewById(id), Gravity.RIGHT, 0, 0);
                mPopupWindow.update();
                startUpdateBackAlpha(true);
            }
        });
    }

    public void show(List<MusicBean> list, MusicBean bean) {
        ((PlayBeanContentView) mContentView).setPlayBean(list, bean);
        ThemeUtils.initTheme(mPopupWindow.getContentView());
        mPopupWindow.showAtLocation(mContext.findViewById(R.id.layout_main), Gravity.BOTTOM, 0, 0);
        mPopupWindow.update();
        startUpdateBackAlpha(true);
    }

    public void show(KeyBean bean, int type) {
        ((GroupBeanContentView) mContentView).setKeyBean(bean, type);
        ThemeUtils.initTheme(mPopupWindow.getContentView());
        mPopupWindow.showAtLocation(mContext.findViewById(R.id.layout_main), Gravity.BOTTOM, 0, 0);
        mPopupWindow.update();
        startUpdateBackAlpha(true);
    }

    PopupWindow getPopupWindow() {
        return mPopupWindow;
    }

    Activity getContext() {return mContext;}

    public void show(List<MusicBean> list, PlayMusicEvent event) {
        ((MusicListPopContentView)mContentView).setMusicEvent(event);
        ((MusicListPopContentView)mContentView).setMusicBeans(list);
        ((MusicListPopContentView)mContentView).notifyDataChanged();
        ThemeUtils.initTheme(mPopupWindow.getContentView());
        mPopupWindow.showAtLocation(mContext.findViewById(R.id.layout_main), Gravity.BOTTOM, 0, 0);
        startUpdateBackAlpha(true);
    }

    void startUpdateBackAlpha(boolean isVisiable) {
        ValueAnimator valueAnimator = null;
        if (isVisiable) {
            valueAnimator = ValueAnimator.ofFloat(1.0f, 0.5f);
        } else {
            valueAnimator = ValueAnimator.ofFloat(0.5f, 1.0f);
        }
        valueAnimator.setDuration(300);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                updateBackAlpha((float) animation.getAnimatedValue());
            }
        });
        valueAnimator.start();
    }

    private void updateBackAlpha(float alpha) {
        WindowManager.LayoutParams layoutParams = mContext.getWindow().getAttributes();
        layoutParams.alpha = alpha;
        mContext.getWindow().setAttributes(layoutParams);
    }
}
